package com.yimi.yinhepda.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.entity.bean.BuildPackageTmepSaveEntity;
import java.util.List;

/**
 * 建包任务暂存适配器
 *
 * @author 000432
 * @date 2018/1/26 0026
 */

public class BuildPackageTempSaveAdapter extends BaseAdapter {
  private Context mContext;
  private List<BuildPackageTmepSaveEntity> mList;

  public BuildPackageTempSaveAdapter(Context context, List<BuildPackageTmepSaveEntity> list) {
    this.mContext = context;
    this.mList = list;
  }

  public int getCheck() {
    for (int i = 0; i < mList.size(); i++) {
      if (mList.get(i).isChecked()) {
        return i;
      }
    }
    return -1;
  }

  @Override public int getCount() {
    return mList.size();
  }

  @Override public Object getItem(int position) {
    return mList.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(final int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(mContext).inflate(R.layout.create_package_task_temp_save_listview_item, null);
      holder.mLinearLayout = (LinearLayout) convertView.findViewById(R.id.create_package_item_layout);
      //包号
      holder.mTaskNo = (TextView) convertView.findViewById(R.id.create_package_item_packNo);
      //目的部门
      holder.mDestDept = (TextView) convertView.findViewById(R.id.create_package_item_destDept);
      //票数
      holder.mPriceNum = (TextView) convertView.findViewById(R.id.create_package_item_priceNum);
      //件数
      holder.mPieceNumber = (TextView) convertView.findViewById(R.id.taskCash_btn_pieceNumber);
      holder.mRadioButton = (RadioButton) convertView.findViewById(R.id.create_package_item_radiobtn);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    BuildPackageTmepSaveEntity bean = mList.get(position);
    holder.mTaskNo.setText(bean.getTaskNo());
    holder.mDestDept.setText(bean.getDestDeptName());
    holder.mPriceNum.setText(bean.getPriceNum() + "");
    holder.mPieceNumber.setText(bean.getPieceNum() + "");
    if (bean.isChecked()) {
      holder.mRadioButton.setChecked(true);
    } else {
      holder.mRadioButton.setChecked(false);
    }

    holder.mLinearLayout.setOnClickListener(new View.OnClickListener() {
      int index = position;

      @Override public void onClick(View v) {
        //设置其他的不选中
        for (BuildPackageTmepSaveEntity item : mList) {
          item.setChecked(false);
        }
        mList.get(index).setChecked(true);
        notifyDataSetChanged();
      }
    });

    holder.mRadioButton.setOnClickListener(new View.OnClickListener() {
      int index = position;

      @Override public void onClick(View v) {
        //设置其他的不选中
        for (BuildPackageTmepSaveEntity item : mList) {
          item.setChecked(false);
        }
        mList.get(index).setChecked(true);
        notifyDataSetChanged();
      }
    });

    return convertView;
  }

  public static class ViewHolder {
    LinearLayout mLinearLayout;
    /** 包号 */
    TextView mTaskNo;
    /** 目的部门 */
    TextView mDestDept;
    /** 票数 */
    TextView mPriceNum;
    /** 件数 */
    TextView mPieceNumber;
    /** 选择按钮 */
    RadioButton mRadioButton;
  }
}
